/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test7.problem2;

import java.util.Date;


class SystemTime {
    private boolean flag = false;
    private int hour;
    private int minute;
    private int second;

    String oddOrEven(int x) {
        return (x%2 == 0)? "even": "odd";
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }
}

class MyThread implements Runnable {

    SystemTime time;
    Thread t;

    public MyThread(SystemTime time) {
        this.time = time;

        t = new Thread(this);
        t.start();
    }

    public void run() {
    }
}

class Thread1 extends MyThread {

    public Thread1(SystemTime time) {
        super(time);
    }

    @Override
    public void run() {
        System.out.println("Thread 1 is running.");

        while (true) {
            synchronized (time) {
                if (!time.isFlag()) {
                    Date date = new Date();
                    time.setHour(date.getHours());
                    time.setMinute(date.getMinutes());
                    time.setSecond(date.getSeconds());

                    time.setFlag(true);
                    time.notify();
                    System.out.println("Time is " + time.getHour() + ":" + time.getMinute() + ":" + time.getSecond() + " ???");
                }

                try {
                    time.wait();
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }
            }
        }
    }
}

class Thread2 extends MyThread {

    public Thread2(SystemTime time) {
        super(time);
    }

    @Override
    public void run() {
        System.out.println("Thread 2 is running.");

        while (true) {
            synchronized (time) {
                while (time.isFlag()) {
                    System.out.println("Hour "+time.getHour()+" is "+time.oddOrEven(time.getHour())+".");
                    System.out.println("Minute "+time.getMinute()+" is "+time.oddOrEven(time.getMinute())+".");
                    System.out.println("Second "+time.getSecond()+" is "+time.oddOrEven(time.getSecond())+".");

                    time.setFlag(false);
                    time.notify();
                }

                try {
                    time.wait();
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }
            }
        }
    }
}

/**
 *
 * @author hoang.pt
 */
public class Demo2 {

    public Demo2() {
        SystemTime time = new SystemTime();
        new Thread1(time);
        new Thread2(time);
    }

    public static void main(String[] args) {
        new Demo2();
    }
}
